package org.elephas.webapp.frontend.validator;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator;
import org.elephas.model.User;
import org.elephas.service.UserFinder;

public class UniqueEmailValidator extends AbstractValidator {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private UserFinder service;

	public UniqueEmailValidator() {
		InjectorHolder.getInjector().inject(this);
	}
	
	@Override
	protected void onValidate(IValidatable validatable) {
		User user = service.findByEmail(validatable.getValue().toString());
		if (user != null) {
			error(validatable);
		}
	}

}
